import { Injectable, Injector } from '@angular/core'
import { IRiskControlQueryParams, IRiskControlSchemePagination } from '@shared/interfaces/post-risk-control'
import { PostRiskScheme, RiskRules } from '@shared/interfaces/post-risk-control'
import { Observable, throwError } from 'rxjs'
import { COMB_URL, COMB_URL2 } from 'app/utils/contraints'
import { HttpClient } from '@angular/common/http'
import { preProcessParams, encodeParams } from '@shared/decorators'
import { IMCC, IMCCQuery } from '@shared/components/mcc-select/mcc-select.interface'
import { IGenericPagination } from '@shared/interfaces/pagination'
import { I18NService } from '@core'
import { NzNotificationService, NzMessageService } from 'ng-zorro-antd'
import { catchError, map } from 'rxjs/operators'
import { IGeneralResponse } from '@shared/interfaces/response'

@Injectable()
export class RiskAlinterService {
  constructor(private http: HttpClient, private injector: Injector) {}

  private get msgSrv(): NzMessageService {
    return this.injector.get(NzMessageService)
  }

  private get i18n(): I18NService {
    return this.injector.get(I18NService)
  }

  @preProcessParams()
  @encodeParams()
  getList(queryParams: IRiskControlQueryParams, str?: string): Observable<IRiskControlSchemePagination> {
    const url = `${COMB_URL}/camel/postrisk/schemes?${str}`
    return this.http.get<IRiskControlSchemePagination>(url)
  }

  create(body: PostRiskScheme): Observable<IGeneralResponse | PostRiskScheme> {
    const url = `${COMB_URL}/camel/postrisk/scheme`
    return this.http.post<any>(url, body, { observe: 'response' }).pipe(
      map(response => {
        const { status } = response
        if (status === 201) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.CREATE_SUCCESS'))
          return response.body
        }
        if (status === 202) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.SUBMIT_TO_AUDIT_SUCCESS'))
          return response.body
        }
        throwError(response)
      }),
      catchError(err => throwError(err)),
    )
  }

  getOne(schemeCode: string): Observable<PostRiskScheme> {
    const url = `${COMB_URL}/camel/postrisk/scheme/${schemeCode}`
    return this.http.get<PostRiskScheme>(url, { withCredentials: true })
  }

  update(schemeCode: string, body: string): Observable<IGeneralResponse | PostRiskScheme> {
    const url = `${COMB_URL}/camel/postrisk/scheme/${schemeCode}`
    return this.http.put<PostRiskScheme>(url, body, { observe: 'response' }).pipe(
      map(response => {
        const { status } = response
        if (status === 200) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.UPDATE_SUCCESS'))
          return response.body
        }

        if (status === 202) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.SUBMIT_TO_AUDIT_SUCCESS'))
          return response.body
        }

        throwError(response)
      }),
      catchError(err => throwError(err)),
    )
  }

  delete(schemeCode: string): Observable<IGeneralResponse> {
    const url = `${COMB_URL}/camel/postrisk/scheme/${schemeCode}`
    return this.http.delete<IGeneralResponse>(url, { observe: 'response' }).pipe(
      map(response => {
        const { status } = response
        if (status === 200) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.DELETE_SUCCESS'))
          return response.body
        }
        if (status === 202) {
          this.msgSrv.success(this.i18n.fanyi('GENERAL.NOTIFICATION.SUBMIT_TO_AUDIT_SUCCESS'))
          return response.body
        }

        throwError(response)
      }),
      catchError(err => {
        return throwError(err)
      }),
    )
  }
  getRules(): Observable<RiskRules[]> {
    const url = `${COMB_URL}/camel/postrisk/rules`
    return this.http.get<RiskRules[]>(url, { withCredentials: true })
  }
  getMccList(): Observable<IGenericPagination<IMCC>> {
    const url = `${COMB_URL}/camel/public/mcc?size=1000`
    return this.http.get<IGenericPagination<IMCC>>(url)
  }
}
